package com.wifirecommenderap;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.app.NotificationManager;
//import android.app.Service;
import android.content.Context;
//import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
//import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.io.IOException;
import java.sql.Date;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class ScanningService extends Service
{
	String thisTag = "Wifi-ScanningService";
	
	final int DEFAULT_DELAY = 3000;  //the default delay:  1 sec for debugging; 10 sec for production  
	final int DEFAULT_SFREQ = 6000;  //the default scanning frequency:  2 sec for debugging; 60 sec for production   

	WifiManager wifi;
	BroadcastReceiver receiver;
	boolean wifiReceiverFinished = true;

	int delay = DEFAULT_DELAY;
	int frequency = DEFAULT_SFREQ;
		
	public int getDelay()
	{
		return delay;
	}

	public int getFrequency()
	{
		return frequency;
	}

	
	public void prefsUpdateHandler()
	//this is the handler to update the preferences when they are changed by the user
	{
		//set all the necessary prefer		
		//frequency = ?
		
		//SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
		//String freqCategory = preferences.getString("list_freq_category", "NULL"); //if value not set return null 

		//updateTimer();
	}
	

	@Override
	public IBinder onBind(Intent intent) 
	//this is a required method
	{
		// TODO Auto-generated method stub
		return null;
	}
	
    private Timer timer;
    private NotificationManager nm;
    LocationManager locMgr;   
    
    private TimerTask task = new TimerTask() 
    {
        @Override
        public void run() 
        {
        	//scan for wifi networks
        	Log.i(thisTag, "Scanned Wifi at " + System.currentTimeMillis());
        	
        	WifiScan();
    		wifi.startScan();
        	Log.i("WifiStart", "Started scanning...");

        	//scan for location
        	//Log.i(thisTag, "Scanned GPS at " + System.currentTimeMillis() );
        	   
        }
    };
  
    
    public void WifiScan()
    {
    	final String TAG = "WiFiDemo";
    	Log.i(TAG, "WifiScan...");
    	
		// Setup WiFi
		wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

		// Register Broadcast Receiver
		if (receiver == null)
			receiver = new WifiScanReceive(this);
		registerReceiver(receiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); 
    }
    
    public void updateTimer()
    {
        this.timer = new Timer();	//maybe this shouldn't go here?
        this.timer.schedule(this.task, getDelay(), getFrequency());
    }
    
    @Override
    public void onCreate() {
    	updateTimer();
    	//this.nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    }
    
    @Override
    public void onStart(Intent intent, int startId) 
    {
        super.onStart(intent, startId);

        //startForeground(1234, new Notification.Builder(this).getNotification()  );  //<<---need to build notification correctly
     
        Log.v(this.thisTag, "ScanningService.onStart():  Service Started");
       
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();

        timer.cancel();
        timer.purge();
        unregisterReceiver(receiver);
        Log.v(this.thisTag, "ScanningService.onDestroy():  Service Stopped");
    }
    
}//ScanningService

